package com.lanou.tipsy.fragment.person;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;

import com.lanou.tipsy.R;
import com.lanou.tipsy.base.BaseFragment;
import com.lanou.tipsy.tools.findLocalSongs.FindSongs;
import com.lanou.tipsy.adapter.playerLists.LocalListAdapter;
import com.lanou.tipsy.entity.playerList.local.Mp3Info;
import com.lanou.tipsy.data.MyEventBus;

import java.util.List;

import de.greenrobot.event.EventBus;


/**
 * Created by FengJun on 15/10/30.
 * 本地歌曲页面
 */
public class MyMusicFragment extends BaseFragment implements View.OnClickListener {
    public static final String TAG = "MyMusicFragment";
    private ListView mListView;
    private List<Mp3Info> localLists;//本地歌曲列表信息
    private FindSongs finder;
    private LocalListAdapter adapter;
    private ImageView backIv;//返回
    private Context context;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.context = context;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fr_local_music_lists, null);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        //调用初始化控件的方法
        initView(getView());
    }

    //初始化控件->设置数据->设置监听
    private void initView(View view) {
        mListView = (ListView) view.findViewById(R.id.lv_my_songs_list);//歌曲列表
        backIv = (ImageView) view.findViewById(R.id.iv_my_songs_list_back);//返回
        backIv.setOnClickListener(this);
        //设置数据
        finder = new FindSongs();
        localLists = finder.getMp3Infos(context.getContentResolver());
        adapter = new LocalListAdapter(getContext(), localLists);
        finder.setListAdpter(getContext(), localLists, mListView);
        //歌曲列表行布局监听
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //向Service传送数据
                EventBus.getDefault().post(new MyEventBus.NetSongs(adapter.getDatas(), position));
            }
        });
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.i("test", TAG + "->onDestory()");
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_my_songs_list_back:
                Log.i("test", TAG + "->MyMusicFragment->返回");
                getActivity().getSupportFragmentManager().popBackStack();
                break;
        }
    }
}